﻿using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.Velloso;
using Gadgeteer.Modules.GHIElectronics;

namespace BluetoothTest
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        Bluetooth bluetooth = new Bluetooth(11);
        Bluetooth.Client client;
        Bluetooth.Host host;
        void ProgramStarted()
        {

            Debug.Print("Program Started");

            // Client Test
            client = bluetooth.ClientMode;

            // Host Test
            //host = bluetooth.HostMode;

            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
            bluetooth.BluetoothStateChanged += new Bluetooth.BluetoothStateChangedHandler(bluetooth_BluetoothStateChanged);
            bluetooth.DataReceived += new Bluetooth.DataReceivedHandler(bluetooth_DataReceived);
            
        }

        void bluetooth_DataReceived(Bluetooth sender, string data)
        {
            Debug.Print("Received: " + data);
        }

        void bluetooth_BluetoothStateChanged(Bluetooth sender, Bluetooth.BluetoothState btState)
        {
            //Debug.Print("New state:" + btState.ToString());
            if (btState == Bluetooth.BluetoothState.Connected)
            {
                client.Send("HELLO");
            }
        }

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            client.EnterPairingMode();
        }
    }
}
